﻿//hoanhtest
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using log4net;
using System.Configuration;

namespace DataDesign.MPP.Utilities
{
    public static class Common
    {
        private static log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static Configuration config = null;

        public static string GetConfigFilePath()
        {
            string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
            return location.Substring(0, location.LastIndexOf("\\")) + "\\" + Constant.FILE_CONFIG;
        }
        public static string GetConnectionString()
        {
            try
            {
                logger.Debug("Begin Get Connection string");
                if (config == null)
                {
                    string _configFile = GetConfigFilePath();
                    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                    fileMap.ExeConfigFilename = _configFile;
                    config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                    logger.Debug("Load config completed");
                }
                else
                {
                    logger.Debug("config != null");
                }
                return config.ConnectionStrings.ConnectionStrings[Constant.MPP2007_CONNECTION_STRING].ConnectionString!=null?config.ConnectionStrings.ConnectionStrings[Constant.MPP2007_CONNECTION_STRING].ConnectionString:null;
            }
            catch (Exception ex)
            {
                logger.Error("ERROR GetConnectionString", ex);
                return string.Empty;
            }
        }
        public static string AppSettingKey(string key)
        {
            try
            {
                logger.Debug("Key: " + key);
                if (config == null)
                {
                    string _configFile = GetConfigFilePath();
                    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                    fileMap.ExeConfigFilename = _configFile;
                    config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                    logger.Debug("Load config completed");
                }
                else
                {
                    logger.Debug("config != null");
                }
                return config.AppSettings.Settings[key].Value;
            }
            catch (Exception ex)
            {
                logger.Error("ERROR AppSettingKey", ex);
                return string.Empty;
            }
        }

        public static string GetWebServicesBaseUrl(string sourceUrl)
        {
            try
            {                
                int index = sourceUrl.IndexOf("/_vti_bin");
                string newUrl = AppSettingKey(Constant.PS_URL);
                newUrl = newUrl + sourceUrl.Substring(index);
                return newUrl;
            }
            catch
            {
                return string.Empty;
            }
        }

        public static bool IsNullOrWhiteSpace(string value)
        {
            return string.IsNullOrEmpty(value);
        }

        #region Extension
        public static bool Contains(this string original, string value, StringComparison comparisionType)
        {
            return original.IndexOf(value, comparisionType) >= 0;
        }
        public static bool IsServerProjectName(this string original)
        {
            return original.StartsWith(Constant.PROJECT_NAME_ON_SERVER_PREFIX);
        }

        public static bool InRange(this IComparable value, object from, object to)
        {
            if (value is Nullable<DateTime>)
            {
                Nullable<DateTime> tmpValue = (Nullable<DateTime>)value;
                Nullable<DateTime> tmpFrom = (Nullable<DateTime>)from;
                Nullable<DateTime> tmpTo = (Nullable<DateTime>)to;

                return (!(value == null && (from != null || to != null)))
                && (from == null || tmpValue.Value.Date.CompareTo(tmpFrom.Value.Date) >= 0)
                && (to == null || tmpValue.Value.Date.CompareTo(tmpTo.Value.Date) <= 0);
            }
            else
                return (!(value == null && (from != null || to != null)))
                && (from == null || value.CompareTo(from) >= 0) && (to == null || value.CompareTo(to) <= 0);
        }

        
        
        #endregion
        
    }
}
//Test again.
